/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

local PrevTime = -1
	local LastTime = 0
	local Disconnected = false
	local StTime = nil
	local SecondsLeft = 39
	
	function PingServer()
		if PrevTime == LastTime then
			Disconnected = true
			LocalPlayer():SetNWBool("disconnected", true)
		else
			Disconnected = false
			SecondsLeft = 40
			LocalPlayer():SetNWBool("disconnected", false)
		end
		PrevTime = LastTime
	
		RunConsoleCommand( "PingServer" )
	end
	timer.Create( "Server Pinger", 3, 0, PingServer )
	
	function ReceivePing( um )
		LastTime = um:ReadLong()
	end
	usermessage.Hook( "_Ping", ReceivePing )
	
	function DrawMessage()
			if Disconnected and SecondsLeft > 11 then
				LocalPlayer():Freeze(true)
				surface.SetDrawColor( Color( 255, 255, 255 ) )
				surface.DrawRect( 0, 0, ScrW(), ScrH() )
				draw.SimpleText( "IT SEEMS WE HAVE LOST CONNECTION TO THE SERVER.", "AtlantisFont", ScrW() / 2, ScrH() / 2, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "PLEASE STAND BY.", "AtlantisFont", ScrW() / 2, (ScrH() / 2) + 30, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "WE WILL CONTINUE TO ATTEMPT TO CONNECT.", "AtlantisFont", ScrW() / 2, (ScrH() / 2) + 60, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "STARGATE: WORLD WARS", "AtlantisFont", ScrW() / 2, 20, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "STARGATE: WORLD WARS", "AtlantisFont", ScrW() / 2, ScrH() - 20, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( SecondsLeft .. " SECONDS LEFT.", "AtlantisFont", ScrW() / 2, (ScrH() / 2) + 90, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
			elseif Disconnected and SecondsLeft <= 10 then
				LocalPlayer():Freeze(true)
				surface.SetDrawColor( Color( 255, 255, 255 ) )
				surface.DrawRect( 0, 0, ScrW(), ScrH() )
				draw.SimpleText( "I'M SORRY, WE SEEM TO HAVE NOT CONNECTED BACK", "AtlantisFont", ScrW() / 2, ScrH() / 2, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "PLEASE STAND BY.", "AtlantisFont", ScrW() / 2, (ScrH() / 2) + 30, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "WE WILL BE DISCONNECTING IN " .. SecondsLeft, "AtlantisFont", ScrW() / 2, (ScrH() / 2) + 60, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "STARGATE: WORLD WARS", "AtlantisFont", ScrW() / 2, 20, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.SimpleText( "STARGATE: WORLD WARS", "AtlantisFont", ScrW() / 2, ScrH() - 20, Color(121, 121, 121, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
			end
	end
	hook.Add( "HUDPaint", "DrawMessage", DrawMessage )
	
	function SetSTime()
		local StTime = os.time()
	end
	timer.Simple( 1, SetSTime )
	
	function SetSeconds()
	
		if Disconnected then
			
			SecondsLeft = SecondsLeft - 1
			
			if SecondsLeft == 4 then RunConsoleCommand("play", WW.Sounds.End2) end
			if SecondsLeft == 0 then RunConsoleCommand("disconnect") end
		end
		
	end
	timer.Create( "Seconds", 1, 0, SetSeconds )